{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import AutoTokenizer, AutoModelForSequenceClassification\n",
    "\n",
    "# 这里用到的不是pipeline\n",
    "pretrain_model_dir = '/media/dengyunfei/6T/data/models/huggingface/roberta-base-finetuned-dianping-chinese'\n",
    "# 从模型目录加载分词器\n",
    "tokenizer = AutoTokenizer.from_pretrained(pretrain_model_dir)\n",
    "# 从模型目录加载模型，该模型指定为自动判断的分类器\n",
    "model = AutoModelForSequenceClassification.from_pretrained(pretrain_model_dir)\n",
    "# 将模型和分词器分别进行存储操作\n",
    "tokenizer.save_pretrained(\"/media/dengyunfei/6T/data/models/huggingface/reberta-tokenizer\")\n",
    "model.save_pretrained(\"/media/dengyunfei/6T/data/models/huggingface/reberta-model\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'transformers.models.bert.tokenization_bert_fast.BertTokenizerFast'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input_ids': tensor([[ 101, 2769, 6230, 2533,  679, 1922, 1962,  102]]), 'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 0, 0]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1]])}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# transformers.models.bert.tokenization_bert_fast.BertTokenizerFast 通过自然选择确定使用快速的bert分词。\n",
    "print(tokenizer.__class__)\n",
    "input_text = \"我觉得不太好\"\n",
    "inputs = tokenizer(input_text, return_tensors=\"pt\")\n",
    "inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers.models.bert.modeling_bert import BertForSequenceClassification\n",
    "# 初始化的时候有一个参数是 config 这说明在读取预训练目录中的模型的过程中，也读取了配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'transformers.models.bert.modeling_bert.BertForSequenceClassification'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "odict_keys(['logits'])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将转化后的输入用模型进行操作 transformers.models.bert.modeling_bert.BertForSequenceClassification 关注它的forward操作。\n",
    "print(model.__class__)\n",
    "res = model(**inputs)\n",
    "res.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.9743, 0.0257]], grad_fn=<SoftmaxBackward0>)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "logits = res.logits\n",
    "logits = torch.softmax(logits, dim=-1)\n",
    "# logits = torch.sigmoid(logits)\n",
    "logits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "pred =  torch.argmax(logits).item()\n",
    "print(pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 'negative (stars 1, 2 and 3)', 1: 'positive (stars 4 and 5)'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.config.id2label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'negative (stars 1, 2 and 3)'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.config.id2label.get(pred)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch24",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
